package com.syh.jn.scm.domain.supplier.entity.base.goods;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.repository.base.goods.GoodsRepository;
import com.syh.jn.scm.dto.supplier.base.goods.goodsOrigin.OriginMto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsOrigin.OriginSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.UUID;

/**
 * 产地
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Origin extends AbstractSimpleAggregate<Long> {
  /**
   * 创建产地
   * @param sto
   * @return
   */
  public static Origin createdOriginBySupply(OriginSto sto){
    ExtAssert.notNull(sto,"新增对象为空");
    ValidationUtils.validate(sto);
    //初始化创建
    Origin origin = Origin.commonCreatedOrigin(sto);
    ExtBeanUtils.copyProperties(sto, origin);
    origin.setUuid(UUID.randomUUID().toString());

    return origin;
  }

  /**
   * 初始化产地
   * @param sto
   * @return
   */
  public static Origin commonCreatedOrigin(OriginSto sto){
    Origin origin = new Origin();
    origin.status = 1;
    Long currentTimeMillis = System.currentTimeMillis();
    origin.createTimeStamp = currentTimeMillis;
    return origin;
  }

  /**
   * 修改产地
   * @param mto
   * @return
   */
  public void modify(OriginMto mto){
    ExtAssert.notNull(mto,"修改对象为空");
    ValidationUtils.validate(mto);
    ExtBeanUtils.copyProperties(mto, this,false);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 删除产地
   */
  public void destroy(){
    //判断产地是否启用
    GoodsRepository goodsQuery = (GoodsRepository) SpringContextUtils.getBean(GoodsRepository.class);
    if(goodsQuery.goodsExistsByColumn("originId",id)){
      throw new BusinessException("该产地已被商品引用，删除失败！");
    }
  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = 1;
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = -1;
  }

  /**
   * 名称
   */
  private String name;
  /**
   * 归属id
   */
  private Long businessEntityId;
  /**
   * 编码
   */
  private String code;
  /**
   * 排序等级
   */
  private Integer orderLevel;
  /**
   * 状态（-1：停用 1：启用）
   */
  private Short status;
}
